$(function () {
    var markid = getUrlString('markid');
    // var markid = 'b88afc62e44094ff46749eb4b55cfadc';
    var page = -1,totalSize = 0,userId;
    let sort;
    if (getCookie('sort')) {
        sort = getCookie('sort')
    }else {
        sort = 0
    }
    //跳往全部
    $('.informationAll').click(function () {
        window.location.href = 'myEggPool.html?mainstate=1&type=1&markid='+markid
    });
    //跳往蛋池
    $('.informationBtn').click(function () {
        window.location.href = 'myEggPoolAll.html?mainstate=1&type=0&markid='+markid
    });
    //跳往预售
    $('.advanceNoticeBtn').click(function () {
        window.location.href = 'myEggPoolPresell.html?mainstate=1&type=1&markid='+markid
    });

    $(document).on('click', '.sort', function() {
        var buttons1 = [
            {
                text: '到期时间从远到近',
                bold: true,
                onClick: function () {
                    $(".sort .rankImg").attr('src','../../res/img/my/myEggPool/up.png');
                    $('#cashCommodityList').empty();//清空容器
                    mRefresh.resetUpLoading(true);
                    sort= 10;
                    page = 0;
                    totalSize = 0;
                    setCookie('sort',sort,0);
                    cashCommodityList(page, sort);
                }
            },
            {
                text: '到期时间从近到远',
                onClick: function () {
                    $(".sort .rankImg").attr('src','../../res/img/my/myEggPool/down.png');
                    $('#cashCommodityList').empty();//清空容器
                    mRefresh.resetUpLoading(true);
                    sort= 11;
                    page = 0;
                    totalSize = 0;
                    setCookie('sort',sort,0);
                    cashCommodityList(page, sort);
                }
            },
            {
                text: '入库时间从近到远',
                onClick: function () {
                    $(".sort .rankImg").attr('src','../../res/img/my/myEggPool/down.png');
                    $('#cashCommodityList').empty();//清空容器
                    mRefresh.resetUpLoading(true);
                    sort= 0;
                    page = 0;
                    totalSize = 0;
                    cashCommodityList(page, sort);
                }
            },
            {
                text: '入库时间从远到近',
                onClick: function () {
                    $(".sort .rankImg").attr('src','../../res/img/my/myEggPool/down.png');
                    $('#cashCommodityList').empty();//清空容器
                    mRefresh.resetUpLoading(true);
                    sort= 1;
                    page = 0;
                    totalSize = 0;
                    setCookie('sort',sort,0);
                    cashCommodityList(page, sort);
                }
            },
            {
                text: '返回',
                onClick: function () {

                }
            }];
        $.modal({
            title: '请选择排列方式',
            verticalButtons: true,
            buttons:buttons1
        });
    });

    var mRefresh = new MiniRefresh({
        container: '#mrefresh', //这里是插件的容器ID
        down: {
            isAuto: false,
            callback: function () {
                // 下拉后的回调事件 ，你可以在这里执行你的后台请求数据函数，达到刷新数据的作用
                // $('#cashCommodityList').empty();//清空容器
                totalSize = 0;
                page = -1;
                mRefresh.endDownLoading();
            }
        }
        ,
        up: {
            isAuto: true,
            callback: function () {
                // 向上拉后的回调事件
                page += 1;
                cashCommodityList(page,sort)

            }
        }
    });

    function cashCommodityList (page,sort) {
        var data = {
            'markid':markid,
            'mainstate':2,
            'type':1,
            'sort':sort,
            'page':page
        };
        getData('post','/niu/apiv3/niu_getuserkuv3.php',data,function (data) {
            console.log(data);
            if(data.resultcode === '0' ){
                var append = '';
                $(data.data).each(function () {
                    append += ' <li>' +
                        '<input class="id" type="hidden" value="'+this.xd_id+'">'+
                        '<input class="xd_pwcode" type="hidden" value="'+this.xd_pwcode+'">'+
                        '<input class="bukuan_status" type="hidden" value="'+this.bukuan_status+'">'+
                        '<input class="is_bukuan" type="hidden" value="'+this.is_bukuan+'">'+
                        '            <div class="pic">' +
                        '                <img src="'+this.pics+'-toysbox11'+'" alt="">' +
                        '                <div class="dayNum">'+this.outtime+'</div>' +
                        '                <div class="saltedLabel">咸蛋</div>' +
                        '            </div>' ;
                    if (this.bukuan_status === '0'){
                        append += '                    <div class="name"><img src="../../res/img/dj2.png" alt=""><span>'+this.name+'</div>' ;
                    }else {
                        append += '                    <div class="name"><span>'+this.name+'</div>' ;
                    }
                        append += '        </li>'
                });
                if(data.pageid === '0'){
                    $('#cashCommodityList').empty().append(append);
                }else {
                    $('#cashCommodityList').append(append);
                }
                //总条数
                totalSize = totalSize + data.pagesize;
                // alert(totalSize > data.datacount);
                if (totalSize > data.datacount)
                    mRefresh.endUpLoading(true);
                else{
                    mRefresh.endUpLoading(false);
                }
            }else {
                $.toast(data.msg)
            }
        })
    }

    $("#cashCommodityList li").live("click", function(){
        var id = $(this).find('.id').val();
        var xd_macode = $(this).find('.xd_pwcode').val();
        var bukuan_status = $(this).find('.bukuan_status').val();
        var is_bukuan = $(this).find('.is_bukuan').val();
        var title = $(this).children('.name').find('span').html();
        cashCommodityListDetail(id,title,xd_macode,bukuan_status,is_bukuan);
    });
    function cashCommodityListDetail(id,title,xd_pwcode,bukuan_status,is_bukuan) {

        var buttons1;
        if (bukuan_status === '0' && is_bukuan === '0'){
            buttons1 = [
                {
                    text: '打开详情',
                    bold: true,
                    onClick: function () {
                        window.location.href = 'dancool://fullweb='+urlView+'saltedEggV2/saltedDetail.html?id='+id;
                    }
                },
                {
                    text: '编辑修改',
                    onClick: function () {
                        window.location.href="dancool://fullweb="+urlView+"my/myEggPoorUpData.html?id="+id+"&markid="+markid;
                    }
                },
                {
                    text: '复制交易密码',
                    onClick: function () {
                        window.location.href="dancool://copydata="+xd_pwcode;
                    }
                },
                {
                    text: '补款',
                    onClick: function () {
                        $.toast('请先下架咸蛋商品再补款')
                    }
                },
                {
                    text: '返回',
                    onClick: function () {

                    }
                }];
        } else {
            buttons1 = [
                {
                    text: '打开详情',
                    bold: true,
                    onClick: function () {
                        window.location.href = 'dancool://fullweb='+urlView+'saltedEggV2/saltedDetail.html?id='+id;
                    }
                },
                {
                    text: '编辑修改',
                    onClick: function () {
                        window.location.href="dancool://fullweb="+urlView+"my/myEggPoorUpData.html?id="+id+"&markid="+markid;
                    }
                },
                {
                    text: '复制交易密码',
                    onClick: function () {
                        window.location.href="dancool://copydata="+xd_pwcode;
                    }
                },
                {
                    text: '返回',
                    onClick: function () {

                    }
                }];
        }
        $.modal({
            title: title,
            verticalButtons: true,
            buttons:buttons1
        });
    }

    //调用app跳往咸蛋记录
    nativeCommand('dancool://titlebar=rightimg|record|nativeCommand("dancool://web='+urlView+'my/saltedEggRecord.html?type=1");');
    //调用app分享小程序
    $('#share').click(function () {
        //获取用户的userid
        getData('post','/niu/apiv3/member_userinfo.php',{'markid':markid},function (data) {
            console.log(data);
            userId = data.data.id;
            nativeCommand('dancool://sharewxsapp=gh_a16176d49b83|||/pages/my/myWarehouseShare/myWarehouseShare?userId='+userId+'|||咸蛋分享|||咸蛋分享说明')
        });
    })

    $('#batchQXGM').click(function () {
        window.location.href = 'dancool://web='+urlView+'my/batchQXGM.html?type=0'
    });
});
